/*
 * @Author: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @Date: 2022-09-14 19:47:12
 * @LastEditors: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @LastEditTime: 2022-09-20 10:07:14
 * @FilePath: \郭招娣\hou\app\controller\articleLists.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @Author: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @Date: 2022-09-12 10:40:26
 * @LastEditors: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @LastEditTime: 2022-09-16 22:12:08
 * @FilePath: \郭招娣\hou\app\controller\home.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
'use strict';

const Controller = require('egg').Controller;
class ArticleListController extends Controller {
    // 获取列表

    async userLists() {
        const { ctx } = this;
        const { pageCode, pageSize, user, email, meta, state } = ctx.request.body;
        console.log(meta, 'kkkkkk')
        let lists = await this.app.mysql.select('user');
        // console.log(lists.length)

        let newLists = lists;
        // console.log(num, 'ii')
        if (user) {
            newLists = newLists.filter(v => v.username.includes(user))
        }
        if (meta) {
            newLists = newLists.filter(v => v.meta == meta)
        }
        if (email) {
            newLists = newLists.filter(v => v.email.includes(email))
        }
        if (state) {
            newLists = newLists.filter(v => v.state == state)
        }

        let total = newLists.length;
        newLists = newLists.slice((pageCode - 1) * pageSize, pageCode * pageSize)
        // console.log(newLists,'gfg')
        if (newLists.length) {
            ctx.body = {
                code: 200,
                total,
                results: newLists
            }
        } else {
            ctx.body = {
                code: 400,
                msg: '获取数据失败'
            }
        }
    }

    // 删除
    async deleteUserLists() {
        const { ctx } = this;
        const { id } = ctx.query;
        const result = await this.app.mysql.delete('user', { id });
        if (result) {
            const list = await this.app.mysql.select('user');
            ctx.body = {
                code: 200,
                msg: '删除成功',
                list
            }
        } else {
            ctx.body = {
                code: 200,
                msg: '删除失败',
                list
            }
        }

    }




    // 多选删除
    async fileDelete() {
        const { ctx } = this;
        const { multipleSelection, pageCode, pageSize } = ctx.request.body;
        const chooseLists = multipleSelection.map(v => {
            return this.app.mysql.delete('user', { id: v.id })
        });

        if (chooseLists) {
            const list = await this.app.mysql.select('user');
            if (list) {
                ctx.body = {
                    code: 200,
                    msg: '删除成功',
                    list: list.slice((pageCode - 1) * pageSize, pageCode * pageSize),
                    total: list.length
                }
            } else {
                ctx.body = {
                    code: 404,
                    msg: '删除失败',
                }
            }
        } else {
            ctx.body = {
                code: 404,
                msg: '多选删除错误',
            }
        }
    }
    // 禁用启用
    async setState(){
        const {ctx}=this;
        const {state,id}=ctx.request.body;
        if(state=='true'){
            const row = {
               state:'false'
            };

            const options = {
                where: {
                    id,
                }
            };
            const result = await this.app.mysql.update('user', row, options); 
            if(result){
                ctx.body = {
                    code: 200,
                    msg: '禁用成功'
                }
            }
        }else if(state=='false'){
            const row = {
                state: 'true'
            };

            const options = {
                where: {
                    id,
                }
            };
            const result = await this.app.mysql.update('user', row, options); 
            if(result){
                ctx.body={
                    code:200,
                    msg:'启用成功'
                }
            }
        }
    }
    // 授权 解除授权
    async authorization() {
        const { ctx } = this;
        const { meta, id } = ctx.request.body;
        if (meta == 'TC') {
            const row = {
                meta: 'USER'
            };

            const options = {
                where: {
                    id,
                }
            };
            const result = await this.app.mysql.update('user', row, options);
            if (result) {
                ctx.body = {
                    code: 200,
                    msg: '解除授权成功'
                }
            }
        } else if (meta == 'USER') {
            const row = {
                meta: 'TC'
            };

            const options = {
                where: {
                    id,
                }
            };
            const result = await this.app.mysql.update('user', row, options);
            if (result) {
                ctx.body = {
                    code: 200,
                    msg: '授权成功'
                }
            }
        }
    }

}

module.exports = ArticleListController;
